package com.android.wellcherish.workclock.util;

import android.content.Context;
import android.support.annotation.CheckResult;
import com.android.wellcherish.workclock.R;
import com.android.wellcherish.workclock.bean.data.GroupRecord;
import com.android.wellcherish.workclock.bean.data.IBaseBean;
import com.android.wellcherish.workclock.bean.data.RecordAdapterContentBean;
import com.android.wellcherish.workclock.bean.data.RecordAdapterHeadBean;
import com.android.wellcherish.workclock.bean.db.Record;
import java.util.ArrayList;
import java.util.List;

/**
 * 封装常用方法的 Util
 *
 * @author 张文琛
 * @date 2019/11/10 10:11
 */
public class CommonUtil {
    private static String content = "第 %d 次打卡";

    /**
     * 判断一个列表是否为空
     *
     * @param list 待判断的列表
     *
     * @return true 表示列表为空，false 表示列表不为空
     * */
    public static boolean isEmptyList(List<? extends Object> list) {
        return list == null || list.isEmpty();
    }

    /**
     * 为需要在 RecyclerView.Adapter 展示的数据设置状态位，并返回结果
     *
     * @param records 记录列表
     * @param isFold 默认得到的数据是否是展开状态
     *
     * @return 设置后的数据集
     * */
    @CheckResult(suggest = "使用该记录列表的值")
    public static List<GroupRecord> getGroupRecords(List<Record> records, boolean isFold) {
        if(isEmptyList(records)) {
            return new ArrayList<>(0);
        }
        List<GroupRecord> result = new ArrayList<>();

        Record lastRecord, currentRecord;
        int id = 0;
        // 当天第一条记录的index
        int count = 1;
        GroupRecord newGroup = null;
        List<IBaseBean> dataList;
        // 1. 将所有数据按天分组
        for(int i = 0; i < records.size(); i++) {
            currentRecord = records.get(i);
            if(currentRecord == null) {
                continue;
            }
            if(i == 0) {
                lastRecord = new Record(0, 0);
            } else {
                lastRecord = records.get(i - 1);
                if(lastRecord == null) {
                    continue;
                }
            }
            if(TimeUtil.isSameDay(lastRecord.getClickSecond(), currentRecord.getClickSecond())) {
                // 如果是同一天，当前布局设置为内容布局
                if(newGroup != null) {
                    newGroup.addDataItem(new RecordAdapterContentBean(id++,
                        String.format(content, ++count),
                        TimeUtil.getTime(currentRecord)));
                }
            } else {
                // 不是同一天，更新计数次数
                count = 1;
                // 更新分组记录
                dataList = new ArrayList<>();
                dataList.add(new RecordAdapterHeadBean(id++,
                    R.drawable.ic_delete_default, TimeUtil.getDate(currentRecord)));
                dataList.add(new RecordAdapterContentBean(id++,
                    String.format(content, count), TimeUtil.getTime(currentRecord)));
                // 实例化新的分组记录
                newGroup = new GroupRecord(i, dataList);
                // 默认是折叠状态
                newGroup.setFold(isFold);
                // 添加最终结果
                result.add(newGroup);
            }
        }

        return result;
    }

    public static int dpToPx(Context context, int size) {
        float scale = context.getResources().getDisplayMetrics().density;
        // int 转化向下取整
        return (int) (size * scale + 0.5f);
    }
}
